package com.jastar.android.game.lego;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.jastar.android.game.lego.brick.Brick;

import java.util.stream.IntStream;

public class LegoBoardView extends View {
    private static final int BOARD_SIZE = 15; // 行列数

    private Paint gridPaint, brickPaint, brickStrokePaint, cursorPaint;
    private Brick[][] bricks;
    private Brick current;
    private int cellSize;

    private int cursorX = BOARD_SIZE / 2;
    private int cursorY = BOARD_SIZE / 2;
    private int lastX, lastY;

    public LegoBoardView(Context context) {
        super(context);
        init();
    }

    public LegoBoardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        gridPaint = new Paint();
        gridPaint.setColor(0xFFD3D3D3);

        brickPaint = new Paint();
        brickPaint.setStyle(Paint.Style.FILL);
        brickPaint.setColor(Color.BLACK);

        brickStrokePaint = new Paint();
        brickStrokePaint.setStyle(Paint.Style.STROKE);
        brickStrokePaint.setColor(0xFF666666);
        brickStrokePaint.setStrokeWidth(1);

        cursorPaint = new Paint();
        cursorPaint.setStyle(Paint.Style.STROKE);
        cursorPaint.setColor(0xFFFFC896);
        cursorPaint.setStrokeWidth(3);

        bricks = new Brick[BOARD_SIZE][BOARD_SIZE];
        current = Brick.getDefault();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        cellSize = size / BOARD_SIZE;
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        drawGrid(canvas);//背板
        drawBricks(canvas); //积木
        drawCursor(canvas); //光标
    }

    private void drawGrid(Canvas canvas) {
        IntStream.range(0, BOARD_SIZE).forEach(i -> IntStream.range(0, BOARD_SIZE).forEach(j -> {
            float r = cellSize / 3F;
            float x = i * cellSize + cellSize / 2F, y = j * cellSize + cellSize / 2F;
            canvas.drawCircle(x, y, r, gridPaint);
        }));
    }

    private void drawBricks(Canvas canvas) {
        IntStream.range(0, BOARD_SIZE).forEach(i -> IntStream.range(0, BOARD_SIZE).forEach(j -> {
            Brick brick = bricks[i][j];
            if (null != brick) {
                brickPaint.setColor(brick.getColor());
                float x = i * cellSize, y = j * cellSize;
                canvas.drawRect(x, y, x + cellSize, y + cellSize, brickPaint); //后续扩展根据BrickType绘制形状
                canvas.drawRect(x, y, x + cellSize, y + cellSize, brickStrokePaint);
            }
        }));
    }

    private void drawCursor(Canvas canvas) {
        float left = cursorX * cellSize;
        float top = cursorY * cellSize;
        float right = (cursorX + 1) * cellSize;
        float bottom = (cursorY + 1) * cellSize;
        canvas.drawRect(left, top, right, bottom, cursorPaint);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        int newX = cursorX;
        int newY = cursorY;

        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                newX = Math.max(0, cursorX - 1);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                newX = Math.min(BOARD_SIZE - 1, cursorX + 1);
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                newY = Math.max(0, cursorY - 1);
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                newY = Math.min(BOARD_SIZE - 1, cursorY + 1);
                break;
            case KeyEvent.KEYCODE_DPAD_CENTER:  //一般遥控的确认键
            case KeyEvent.KEYCODE_BUTTON_1:     //某些设备的确认键
            case KeyEvent.KEYCODE_ENTER:        //回车，兼容模拟器调试
                updateCell();
                invalidate();
                return true;
            default:
                return super.onKeyDown(keyCode, event);
        }

        if (newX != cursorX || newY != cursorY) {
            cursorX = newX;
            cursorY = newY;
            invalidate();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void updateCell() {
        if (lastX == cursorX && lastY == cursorY) updateCurrent(); //如果连续点击，改变current
        bricks[cursorX][cursorY] = current;
        lastX = cursorX;
        lastY = cursorY;
    }

    private void updateCurrent() {
        if (null == current) {
            current = Brick.getDefault();
        } else if (current.isLastColor()) {
            current = null;
        } else {
            current = current.clone();
            current.nextColor();
        }
    }

}